package org.jeecg.cn.edu.scau.vm.config;

import com.alibaba.fastjson.JSON;
import org.jeecg.cn.edu.scau.vm.container.domain.VmContainer;
import org.jeecg.cn.edu.scau.vm.container.enums.ContainerStatusEnum;
import org.jeecg.cn.edu.scau.vm.container.response.CreateContainerResponse;
import org.jeecg.cn.edu.scau.vm.container.response.RemoveContainerResponse;
import org.jeecg.cn.edu.scau.vm.container.response.StopContainerResponse;
import org.jeecg.cn.edu.scau.vm.container.service.VmContainerBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Component("RabbitListeners")
public class RabbitListeners {
    @Resource
    private VmContainerBizService vmContainerBizService;
    private static final Logger logger = LoggerFactory.getLogger(RabbitListeners.class);

    @RabbitListener(queues = "container_create_success_queue")
    public void handleCreateContainerSuccess(Message message) {
        // Json序列化对象
        String rawMessage = new String(message.getBody());
        logger.info("收到容器创建成功消息,序列后的对象为:{}", rawMessage);
        // 还原对象信息,并处理相关逻辑
        CreateContainerResponse response = JSON.parseObject(rawMessage, CreateContainerResponse.class);
        vmContainerBizService.handleCreateContainerSuccess(response, null);
    }

    @RabbitListener(queues = "container_create_failed_queue")
    public void handleCreateContainerFailed(Message message) {
        // Json序列化对象
        String rawMessage = new String(message.getBody());
        logger.info("收到容器创建失败消息,序列后的对象为:{}", rawMessage);
        // 1. 还原对象信息
        CreateContainerResponse response = JSON.parseObject(rawMessage, CreateContainerResponse.class);
        VmContainer vmContainer = VmContainer.builder()
                .id(response.getTableId())
                .status(ContainerStatusEnum.CREATE_FAILED.getCode())
                .updateTime(LocalDateTime.now())
                .build();
        vmContainerBizService.updateById(vmContainer);
        logger.info("创建容器失败!容器状态更新完毕,{}", vmContainer);
    }

    @RabbitListener(queues = "container_stop_success_queue")
    public void handleStopContainerSuccess(Message message) {
        // 1. 解析信息
        String rawMessage = new String(message.getBody());
        logger.info("收到容器停止成功消息,序列后的对象为:{}", rawMessage);
        // 2. 还原对象
        StopContainerResponse response = JSON.parseObject(rawMessage, StopContainerResponse.class);
        vmContainerBizService.updateStatus(response.getTableId(), ContainerStatusEnum.STOPPED.getCode(), LocalDateTime.now());
        logger.info("停用容器{}成功!容器状态更新完毕,{}", response.getContainerId(), response);
    }

    @RabbitListener(queues = "container_stop_failed_queue")
    public void handleStopContainerFailed(Message message) {
        // 1. 解析信息
        String rawMessage = new String(message.getBody());
        logger.info("收到容器停止失败消息,序列后的对象为:{}", rawMessage);
        // 2. 还原对象
        StopContainerResponse response = JSON.parseObject(rawMessage, StopContainerResponse.class);
        boolean result = vmContainerBizService.updateStatus(response.getTableId(), ContainerStatusEnum.STOP_FAILED.getCode(), LocalDateTime.now());
        logger.info("停用容器{}失败!容器状态更新完毕,{},更新结果:{}", response.getContainerId(), response, result);
    }

    @RabbitListener(queues = "container_start_success_queue")
    public void handleStartContainerSuccess(Message message) {
        // 1. 解析信息
        String rawMessage = new String(message.getBody());
        logger.info("收到容器启动成功消息,序列后的对象为:{}", rawMessage);
        // 2. 还原对象
        StopContainerResponse response = JSON.parseObject(rawMessage, StopContainerResponse.class);
        vmContainerBizService.updateStatus(response.getTableId(), ContainerStatusEnum.RUNNING.getCode(), LocalDateTime.now());
        logger.info("启动容器{}成功!容器状态更新完毕,{}", response.getContainerId(), response);
    }

    @RabbitListener(queues = "container_start_failed_queue")
    public void handleStartContainerFailed(Message message) {
        // 1. 解析信息
        String rawMessage = new String(message.getBody());
        logger.info("收到容器启动失败消息,序列后的对象为:{}", rawMessage);
        // 2. 还原对象
        StopContainerResponse response = JSON.parseObject(rawMessage, StopContainerResponse.class);
        vmContainerBizService.updateStatus(response.getTableId(), ContainerStatusEnum.DOWN.getCode(), LocalDateTime.now());
        logger.info("启动容器{}失败!容器状态更新完毕,{}", response.getContainerId(), response);
    }

    @RabbitListener(queues = "container_remove_success_queue")
    public void handleRemoveContainerSuccess(Message message) {
        // 1. 解析信息
        String rawMessage = new String(message.getBody());
        logger.info("收到容器删除成功消息,序列后的对象为:{}", rawMessage);
        // 2. 还原对象
        RemoveContainerResponse response = JSON.parseObject(rawMessage, RemoveContainerResponse.class);
        vmContainerBizService.removeContainerByContainerId(response.getContainerId());
        logger.info("删除容器{}成功!容器从表中删除,{}", response.getContainerId(), response);
    }

    @RabbitListener(queues = "container_remove_failed_queue")
    public void handleRemoveContainerFailed(Message message) {
        // 1. 解析信息
        String rawMessage = new String(message.getBody());
        logger.info("收到容器删除失败消息,序列后的对象为:{}", rawMessage);
        // 2. 还原对象
        StopContainerResponse response = JSON.parseObject(rawMessage, StopContainerResponse.class);
        vmContainerBizService.updateStatus(response.getTableId(), ContainerStatusEnum.DOWN.getCode(), LocalDateTime.now());
        logger.info("删除容器{}失败!容器状态设置为异常!{}", response.getContainerId(), response);
    }


}
